const app = getApp()
const util = require('../../../../../utils/util.js');
const api = require('../../../../../config/api.js');
Page({
  data: {
    fixwz:'',//判断置顶时的位置
    selGroup: null,//选中的分组
    circleid: '',// 圈子id
    themeListInfo: {},// 数据库的整个主题信息
    //辅助
    auxiliary: { "showFunctionPop": false, 'showFunctionPopIndex': '' },
  },
  /**
   * 页面加载事件
   */
  onLoad: function (options) {
    var _this = this;
    var query = wx.createSelectorQuery();
    query.select('.fixwz').boundingClientRect(function (rect) {
      var wz = parseInt(rect.height);
      _this.setData({
        fixwz: parseInt(wz + 55)
      })
    }).exec();
    _this.setData({
      circleid: options.circleid
    })
  },
  /**
   * 允许成员一天闯多关
   */
  isCheckpointDayChange: function () {
    wx.showLoading({
      title: '请求中',
    })
    var _this = this;
    var circleid = _this.data.circleid;
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.circleid = circleid;
        data.openid = openid;
        data.access_token = res.access_token;
        data.type = 1;// 修改类型：1修改总的  2修改单个组
        var isCheckpointDay = _this.data.themeListInfo.isCheckpointDay;
        if (isCheckpointDay == 1){
          // 关闭
          data.isCheckpointDay = 0;
          data.isCheckpoint = 0;
        }else{
          data.isCheckpointDay = 1;
          data.isCheckpoint = 0;
        }
        _this.updateCheckpointQuery(data);
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this;
    _this.getThemeList();
  },
  /**
   * 允许成员任意闯多关
   */
  isCheckpointChange: function () {
    wx.showLoading({
      title: '请求中',
    })
    var _this = this;
    var circleid = _this.data.circleid;
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.circleid = circleid;
        data.openid = openid;
        data.access_token = res.access_token;
        data.isCheckpointDay = _this.data.themeListInfo.isCheckpointDay;
        data.type = 1;// 修改类型：1修改总的  2修改单个组
        var isCheckpoint = _this.data.themeListInfo.isCheckpoint;
        if (isCheckpoint == 1) {
          // 关闭
          data.isCheckpoint = 0;
        } else {
          data.isCheckpoint = 1;
        }
        _this.updateCheckpointQuery(data);
      })
    }
  },

  /**
   * 修改参加主题设置
   */
  updateCheckpointQuery: function(data){
    var _this = this;
    util.post(api.Interest_UpdateCheckpointApi, data).then(function (res) {
      wx.hideLoading();
      if (res.code == 200) {
        _this.setData({
          ["themeListInfo.isCheckpoint"]: data.isCheckpoint,
          ["themeListInfo.isCheckpointDay"]: data.isCheckpointDay
        })
      }else{
        util.showToast(res.message, "none");
      }
    })
  },

  /**
   * 获取主题、主题组信息
   */
  getThemeList: function(){
    wx.showLoading({
      title: '请求中',
    })
    var _this = this;
    var circleid = _this.data.circleid;
    var openid = app.globalData.logininfo.openid;
    var data = {};
    data.circleid=circleid;
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        _this.getThemeListQuery(api.Interest_GetThemeListApi,data);
      })
    }else{
      _this.getThemeListQuery(api.Interest_GetThemeList, data);
    }
  },
  /**
   * 获取主题、主题组信息的请求
   */
  getThemeListQuery: function (url, data) {
    var _this = this;
    util.post(url, data).then(function (res) {
      wx.hideLoading()
      if (res.code == 200) {
        _this.setData({
          themeListInfo: res.result
        })
      }
    })
  },
  /**
   * 获取滚动条当前位置
   */
  onPageScroll: function (e) {
    var _this = this;
    var scrollTop = parseInt(e.scrollTop);
    var wz = _this.data.fixwz
    if (scrollTop >= wz) {
      _this.setData({
        positionFixed: true
      })
    } else {
      _this.setData({
        positionFixed: false
      })
    }
  },  
  /**
   * 分组选择
   */
  selectGroup: function(e){
    var _this = this
    var index = e.currentTarget.dataset.index;
    this.setData({
      selGroup: index,
      groupName: _this.data.themeListInfo.themeGroupList[index].name
    })
  },

  /**
   * 主题移动
   */
  themeMobile: function (e) {
    wx.showLoading({
      title: '请求中',
    })
    var _this = this;
    var themeList = _this.data.themeListInfo.themeList;
    var index = e.currentTarget.dataset.index;
    var circleid = _this.data.circleid;
    var type = e.currentTarget.dataset.type;
    var openid = app.globalData.logininfo.openid;
    var themeid = themeList[index].id;
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.openid = openid;
        data.themeid = themeid;
        data.type = type;
        data.circleid = circleid;
        data.access_token = res.access_token;
        util.post(api.Interest_ThemeMobileApi, data).then(function (res) {
          wx.hideLoading();
          if(res.code == 200){
            _this.getThemeList();
          }else{
            util.showToast(res.message, "none");
          }
        })
      })
    }
  },

  /**
   * 进入主题编辑页面
   */
  navigateToMakeTheme: function (e) {
    var _this = this;
    var circleid = _this.data.circleid;
    var index = e.currentTarget.dataset.index;
    if(index != null && String(index).length > 0){
      var copy = e.currentTarget.dataset.copy;
      var themeList = _this.data.themeListInfo.themeList;
      var themeid = themeList[index].id;
      _this.closeMC();
      if (copy == "copy") {
        wx.navigateTo({
          url: '../makeTheme/makeTheme?o_type=copy&themeid=' + themeid + '&circleid=' + circleid + '&type=' + _this.data.themeListInfo.themeType,
        })
      }else{
        wx.navigateTo({
          url: '../makeTheme/makeTheme?o_type=update&themeid=' + themeid + '&circleid=' + circleid + '&type=' + _this.data.themeListInfo.themeType,
        })
      }
    } else {
      _this.closeMC();
      var themeType= _this.data.themeListInfo.themeType;
      if (themeType == null){
        wx.navigateTo({
          url: '../../theme/themeIndex?circleid=' + circleid,
        })
      }else{
        wx.navigateTo({
          url: '../makeTheme/makeTheme?o_type=create&themeid=&circleid=' + circleid + '&type=' + _this.data.themeListInfo.themeType,
        })
      }
    }
  }, 
  /**
   * 创建主题组
   */
  gotoSetGroup: function () {
    var _this = this;
    wx.navigateTo({
      url: 'setThemeGroup/setThemeGroup?addTheme=0&groupid=&circleid=' + _this.data.circleid,
    })
  },
  /**
   * 进入主题组管理
   */
  gotoGroupDetails: function (e) {
    var _this = this;
    var groupid = e.currentTarget.dataset.groupid;
    wx.navigateTo({
      url: 'groupDetails/groupDetails?groupid=' + groupid + '&circleid=' + _this.data.circleid,
    })
  },
  /**
   * 进入搜索页
   */
  gotoSeek: function() {
    var _this = this;
    wx.navigateTo({
      url: 'searchTheme/searchTheme?circleid=' + _this.data.circleid + '&type=' + _this.data.themeListInfo.themeType,
    })
  },
  /**
   * 进入设置优惠券
   */
  gotoDiscount: function () {
    var _this = this;
    wx.navigateTo({
      url: 'discount/discount',
    })
  },
  /**
   * 主题设置页
   */
  navigateToThemeSet: function () {
    var _this = this;
    var index = _this.data.auxiliary.showFunctionPopIndex;
    var themeList = _this.data.themeListInfo.themeList;
    var themeid = themeList[index].id;
    var contentType = themeList[index].contentType;
    var circleid = _this.data.circleid;
    _this.closeMC();
    wx.navigateTo({
      url: '../makeTheme/themeSet/themeSet?circleid=' + circleid + '&themeid=' + themeid + '&contentType=' + contentType,
    })
  }, 
  /**
   * 打开功能面板
   */
  openFunctionPop: function (e) {
    var _this = this;
    var index = e.currentTarget.dataset.index;
    this.setData({
      ["auxiliary.showFunctionPop"]: true,
      ["auxiliary.showFunctionPopIndex"]: index,
    })
  },
  /**
   * 关闭所有弹窗
   */
  closeMC: function () {
    var _this = this;
    _this.setData({
      ["auxiliary.showFunctionPop"]: false,
      ["auxiliary.showFunctionPopIndex"]: ""
    })
  },

  /**
   * 删除主题
   */
  deleteTheme: function(){
    var _this = this;
    var index = _this.data.auxiliary.showFunctionPopIndex;
    var themeList = _this.data.themeListInfo.themeList;
    var themeid = themeList[index].id;
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.themeid = themeid;
        data.openid = openid;
        data.access_token = res.access_token;
        util.post(api.Interest_DeleteThemeApi, data).then(function (res) {
          if (res.code == 200) {
            _this.closeMC();
            util.showToast("删除成功", "success");
            _this.getThemeList();
          } else {
            util.showToast(res.message, "none");
          }
        })
      })
    }
  },

  /**
   * 预览主题详情
   */
  navigateToThemeResult: function(e){
    var _this = this;
    var themeid = e.currentTarget.dataset.themeid;
    var contenttype = e.currentTarget.dataset.contenttype;
    if (contenttype == 1 || contenttype == 2 || contenttype == 3){
      wx.navigateTo({
        url: '../themeDetails/themeResult/themeResult?previewType=3&themeid=' + themeid
      })
    }else{
      wx.navigateTo({
        url: '../goodsDetails/goodsDetails?themeid=' + themeid
      })
    }
  }
})